package cn.wu.reggie.controller;

import cn.wu.reggie.common.R;
import cn.wu.reggie.entity.ShoppingCart;
import cn.wu.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @RequestMapping("add")
    public R add2Cart(@RequestBody ShoppingCart paramCart, HttpSession session){
        // 1 给购物项设置用户id
        Long userId = (Long) session.getAttribute("user");
        paramCart.setUserId(userId);

        // 2 调用service完成加入购物车操作
        return shoppingCartService.add2Cart(paramCart);
    }

    @GetMapping("list")
    public R showCart(HttpSession session){
        // 1 给购物项设置用户id
        Long userId = (Long) session.getAttribute("user");
        return shoppingCartService.showCart(userId);
    }

    @DeleteMapping("clean")
    public R cleanCart(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        return shoppingCartService.cleanCart(userId);
    }

}
